package org.javaforever.gatescore.reports.verb;

import java.util.ArrayList;
import java.util.List;

import org.javaforever.gatescore.core.FrontCodeBlock;
import org.javaforever.gatescore.core.FrontDomain;
import org.javaforever.gatescore.core.FrontDropdown;
import org.javaforever.gatescore.core.FrontField;
import org.javaforever.gatescore.core.FrontMethod;
import org.javaforever.gatescore.core.FrontVerb;
import org.javaforever.gatescore.core.Statement;
import org.javaforever.gatescore.core.Writeable;
import org.javaforever.gatescore.core.WriteableUtil;
import org.javaforever.gatescore.utils.FieldUtil;
import org.javaforever.gatescore.utils.StringUtil;

public class ListActiveCompareOption extends FrontVerb{	
	private static final long serialVersionUID = 1L;
	protected FrontDomain planDomain;
	protected FrontDomain actionDomain;
	protected List<FrontField> planxAxisFields = new ArrayList<>();
	protected FrontField planyName;
	protected List<FrontField> actionxAxisFields = new ArrayList<>();
	protected FrontField actionyName;
	
	public ListActiveCompareOption(FrontDomain planDomain, List<FrontField> planxAxisFields, FrontField planyName, FrontDomain actionDomain,List<FrontField> actionxAxisFields, FrontField actionyName) {
		super();
		this.domain = planDomain;
		this.planDomain = planDomain;
		this.actionDomain = actionDomain;
		this.planxAxisFields = planxAxisFields;
		this.planyName = planyName;
		this.actionxAxisFields = actionxAxisFields;
		this.actionyName = actionyName;
		this.setVerbName("ListActiveCompare"+this.domain.getCapFirstPlural()+"Option");
		this.setLabel("列出活跃记录图表");
	}
	@Override
	public FrontMethod generateControllerMethod() throws Exception {
		FrontMethod method = new FrontMethod();
		method.setStandardName("listActiveCompare"+StringUtil.capFirst(this.planDomain.getCapFirstPlural()+"Option"));
		//method.addSignature(new Signature(1,"chartstype","var"));
		List<Writeable> sList = new ArrayList<Writeable>();
		sList.add(new Statement(1000L,0,"listActiveCompare"+this.planDomain.getCapFirstPlural()+"Option(chartstype) {"));
		sList.add(new Statement(2000L,1,"let option = {}"));
		sList.add(new Statement(3000L,1,"option.title = { text: '' }"));
		sList.add(new Statement(4000L,1,"option.tooltip = {}"));
		sList.add(new Statement(5000L,1,"option.xAxis = { data: ["+FieldUtil.genFieldArrayTextSingleQuotedStr(this.planxAxisFields)+"] }"));
		sList.add(new Statement(6000L,1,"option.yAxis = {}"));
		sList.add(new Statement(7000L,1,"let planData = []"));
		sList.add(new Statement(8000L,1,"let legendData = []"));
		sList.add(new Statement(10000L,1,"for (let i = 0; i < this.active"+this.planDomain.getCapFirstPlural()+".length; i++) {"));
		sList.add(new Statement(11000L,2,"let item = this.active"+this.planDomain.getCapFirstPlural()+"[i]"));
		sList.add(new Statement(12000L,2,"let itemdata = {}"));
		
		if (this.planyName instanceof FrontDropdown) {
			sList.add(new Statement(13000L,2,"itemdata.name = this.translate"+((FrontDropdown)this.planyName).getTarget().getCapFirstDomainName()+"(item."+this.planyName.getLowerFirstFieldName()+")"));
		}else {
			sList.add(new Statement(13000L,2,"itemdata.name = item."+this.planyName.getLowerFirstFieldName()));
		}
		sList.add(new Statement(15000L,2,"legendData.push(itemdata.name)"));
		sList.add(new Statement(16000L,2,"itemdata.type = chartstype"));
		
		sList.add(new Statement(17000L,2,"var itemdatadata = ["+FieldUtil.genFieldArrayLowerFirstNameWithPrefixStr(this.planxAxisFields, "item.")+"]"));
		
		sList.add(new Statement(18000L,2,"itemdata.data = itemdatadata"));
		sList.add(new Statement(19000L,2,"planData.push(itemdata)"));
		sList.add(new Statement(20000L,1,"}"));
		
		sList.add(new Statement(21000L,1,"for (let i = 0; i < this.active"+this.actionDomain.getCapFirstPlural()+".length; i++) {"));
		sList.add(new Statement(22000L,2,"let item = this.active"+this.actionDomain.getCapFirstPlural()+"[i]"));
		sList.add(new Statement(23000L,2,"let itemdata = {}"));
		
		if (this.actionyName instanceof FrontDropdown) {
			sList.add(new Statement(24000L,2,"itemdata.name = this.translate"+((FrontDropdown)this.actionyName).getTarget().getCapFirstDomainName()+"(item."+this.actionyName.getLowerFirstFieldName()+")"));
		}else {
			sList.add(new Statement(24000L,2,"itemdata.name = item."+this.actionyName.getLowerFirstFieldName()));
		}
		sList.add(new Statement(27000L,2,"legendData.push(itemdata.name)"));
		sList.add(new Statement(28000L,2,"itemdata.type = chartstype"));		
		sList.add(new Statement(29000L,2,"var itemdatadata = ["+FieldUtil.genFieldArrayLowerFirstNameWithPrefixStr(this.actionxAxisFields, "item.")+"]"));
		sList.add(new Statement(30000L,2,"itemdata.data = itemdatadata"));
		sList.add(new Statement(31000L,2,"planData.push(itemdata)"));
		sList.add(new Statement(32000L,1,"}"));
		sList.add(new Statement(33000L,1,"option.legend = {}"));
		sList.add(new Statement(34000L,1,"option.legend.data = legendData"));
		sList.add(new Statement(35000L,1,"option.series = planData"));
		sList.add(new Statement(36000L,1,"this.option = option"));
		sList.add(new Statement(37000L,0,"},"));

		method.setMethodStatementList(WriteableUtil.merge(sList));
		return method;
	}

	@Override
	public FrontMethod generateApiMethod() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public FrontCodeBlock generateRouteBlockBlock() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	public FrontDomain getPlanDomain() {
		return planDomain;
	}
	public void setPlanDomain(FrontDomain planDomain) {
		this.planDomain = planDomain;
	}
	public FrontDomain getActionDomain() {
		return actionDomain;
	}
	public void setActionDomain(FrontDomain actionDomain) {
		this.actionDomain = actionDomain;
	}
	public List<FrontField> getPlanxAxisFields() {
		return planxAxisFields;
	}
	public void setPlanxAxisFields(List<FrontField> planxAxisFields) {
		this.planxAxisFields = planxAxisFields;
	}
	public FrontField getPlanyName() {
		return planyName;
	}
	public void setPlanyName(FrontField planyName) {
		this.planyName = planyName;
	}
	public List<FrontField> getActionxAxisFields() {
		return actionxAxisFields;
	}
	public void setActionxAxisFields(List<FrontField> actionxAxisFields) {
		this.actionxAxisFields = actionxAxisFields;
	}
	public FrontField getActionyName() {
		return actionyName;
	}
	public void setActionyName(FrontField actionyName) {
		this.actionyName = actionyName;
	}
	@Override
	public FrontMethod generateControllerMethod(String indexSuffix) throws Exception {
		return generateControllerMethod();
	}
}
